package com.ygo.feihua;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.os.*;
import android.support.v7.app.*;
import android.support.v7.widget.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import cn.bmob.v3.*;
import com.ygo.feihua.*;
import com.ygo.feihua.BmobTable.*;
import com.ygo.feihua.Management.*;
import com.ygo.feihua.adapter.*;
import com.ygo.feihua.type.*;
import java.util.*;

import android.support.v7.widget.Toolbar;
import cn.bmob.v3.datatype.*;
import com.xiaomi.mistatistic.sdk.*;

public class RobotFeihua extends AppCompatActivity
{

	private Gongju gj;
	private RecyclerView rt_rec;
	private EditText rt_msg;
	private Button rt_send;
	
	AdapterRobot adprt;
	List<RobotLiaotian> data=new ArrayList<RobotLiaotian>();

	private UserManagement um;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.robot_feihua);
		csh();
		
		rt_send.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					
					if(um.getUser()==null){
						um.logInUser(RobotFeihua.this);
						gj.show("还没登录呢");
					}else{
					String sendmessage=rt_msg.getText().toString();
					//rt_msg.setText("");
					RobotLiaotian rol1=new RobotLiaotian();
					rol1.setMsg(sendmessage);
					rol1.setName(um.getUser().getUsername());
					rol1.setUser(um.getUser());
					rol1.setType(1);
					data.add(rol1);
					
					int start=sendmessage.indexOf("查询卡[");
					int end=-1;
					if(start!=-1){
						end=sendmessage.indexOf("]",start);
					}
					if(start==-1||end==-1){
					 RobotManagement.getMessage(RobotFeihua.this,adprt,sendmessage);
					}else{
						//获取卡片
						SQLiteDatabase sjk2=SQLiteDatabase.openOrCreateDatabase(Gongju.PATH_EZ_MANAGEMENT+"cards.cdb", null);//数据库不存在则创建，存在则取得对象的实例
						Cursor datas1=sjk2.query("texts", new String[]{"id","name","desc"}, "name LIKE ? ", new String[]{"%" + sendmessage.substring(start+4,end)+"%"}, null, null, null);
						RobotLiaotian rolt=new RobotLiaotian();

						rolt.setName("小废");
						rolt.setType(0);
						
						if (datas1.getCount() != 0)
						{
							
							//获取集合的个数，如果不为0(没查到则为0)
							datas1.moveToNext();//移动到第一行，默认是0，没有写这句，直接获取值会抛异常
							if(datas1.getString(0)!=null&&!datas1.getString(0).equals("")){
								rolt.setMsg("[kp"+datas1.getString(0)+"]");
							}else{
								rolt.setMsg("未查询到该卡");
							}
						}
						else
						{
							rolt.setMsg("未查询到该卡");
						}	
						
						adprt.sx(rolt);
						
						
						datas1.close();
					}
						sx();
					
				}
					// TODO: Implement this method
				}
			});
		
	}
	

	@Override
	protected void onResume(){
		// TODO: Implement this method
		super.onResume();
		SdkInitManagement.getDx().initMiStats();
		MiStatInterface.recordPageStart(this, "小废");
	}

	@Override
	protected void onPause(){
		// TODO: Implement this method
		super.onPause();
		MiStatInterface.recordPageEnd();
	}
	
	
	public void  sx(){
		rt_rec.scrollToPosition(adprt.getItemCount() - 1);
		//rt_list.setSelection(adprt.getCount() - 1);
		rt_msg.setText("");
		
		
	}
	
	private void csh(){
		Toolbar tool=(Toolbar) findViewById(R.id.toolbar);
		rt_rec=(RecyclerView) findViewById(R.id.rt_rec);
		rt_msg=(EditText) findViewById(R.id.rt_msg);
		rt_send=(Button) findViewById(R.id.rt_send);
	
		gj=Gongju.getdx(this);
		um=UserManagement.getDx();
		RobotLiaotian rol=new RobotLiaotian();
		rol.setMsg("泥嚎我是小废~");
		rol.setName("小废");
		rol.setType(0);
		data.add(rol);
		
		gj.cshToolbar(tool,"辅助机器人-小废");
		
		//LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
		//linearLayoutManager.setStackFromEnd(true); //关键 设置此项，当软键盘弹出时，布局会自动顶上去，在结合AndroidManifest.xml设置属性
		rt_rec.setLayoutManager(new LinearLayoutManager(this)); 
		
		adprt=new AdapterRobot(RobotFeihua.this,data);
		rt_rec.setAdapter(adprt);
		
	}
	
	
}
